package com.example.demo.controller;

import com.example.demo.entity.Result;
import com.example.demo.entity.Student;
import com.example.demo.service.StuService;
import com.example.demo.util.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by 13795 on 2017/7/5.
 */
@RestController
public class StuController {
    @Autowired
    public StuService stuService;
    @PostMapping(value = "/findAllStudent")
    public List<Student> findAllStudent(){
        List<Student> list=stuService.findAllStudent();
        return list;
    }
    @PostMapping(value="/addStudent")
    public Result addStudent(@Valid Student student, BindingResult result){
        if (result.hasErrors()){
            return ResultUtils.fails(result.getFieldError().getDefaultMessage());
        }
        stuService.saveStudent(student);
        return ResultUtils.success();
    }
    @PostMapping(value = "/deleStudent/{id}")
    public Result deleteStudent(@PathVariable Integer id){
        stuService.deleteStudent(id);
        return ResultUtils.success();
    }
}
